package Controllers;

import Configs.Config;
import com.db4o.ObjectSet;
import com.db4o.ext.DatabaseClosedException;
import com.db4o.ext.DatabaseReadOnlyException;
import com.db4o.ext.Db4oIOException;
import com.db4o.query.Predicate;
import java.awt.HeadlessException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import models.ChiTietGia;
import models.GiaPhong;
import models.LoaiPhong;
import models.NhaTro;

public class ChiTietGiaController {

    private final Config conn;
    private BieuThucChinhQuy btcq;
    private MyTableFormat myFormat;

    public ChiTietGiaController(Config conn) {
        this.conn = conn;
    }

    public void Show(JTable tbdsChiTietGia, String soGPDKKD, int idLoaiPhong) {
        this.myFormat = new MyTableFormat();
        DefaultTableModel model = (DefaultTableModel) tbdsChiTietGia.getModel();
        ObjectSet<ChiTietGia> chiTietGias = this.conn.getConn().query(new Predicate<ChiTietGia>() {
            @Override
            public boolean match(ChiTietGia et) {
                return et.getNhatro().getGPKD().equals(soGPDKKD) && 
                        et.getLoaiphong().getIdloaiphong() == idLoaiPhong;
            }
        });
        int stt = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        for (ChiTietGia chiTietGia : chiTietGias) {
            Object[] row = {++stt, chiTietGia.getGiaphong().getMagia(), XuLyCurrency.priceWithDecimal(chiTietGia.getGiaphong().getGia()+""), sdf.format(chiTietGia.getThoidiemapdung())};
            model.addRow(row);
        }
        this.myFormat.resizeFitRows(tbdsChiTietGia);
    }

    public void Add() {

    }

    public void Edit() {

    }

    public boolean Delete(int idgia, long giaphong, String ngayapdung) throws ParseException {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            Date ngayApDung = sdf.parse(ngayapdung);
            ChiTietGia xoa = this.conn.getConn().query(new Predicate<ChiTietGia>() {
                @Override
                public boolean match(ChiTietGia et) {
                    Date ngayCSDL = null;
                    try {
                        ngayCSDL = sdf.parse(sdf.format(et.getThoidiemapdung()));
                    } catch (ParseException ex) {
                        Logger.getLogger(ChiTietGiaController.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    return et.getGiaphong().getGia() == giaphong && DateNTime.compareDate(ngayCSDL, ngayApDung) == 0;
                }
            }).next();
            GiaPhong giaPhong = xoa.getGiaphong();
            NhaTro nhaTro = xoa.getNhatro();
            LoaiPhong loaiPhong = xoa.getLoaiphong();
            nhaTro.removeChiTietGia(xoa);
            giaPhong.removeChiTietGia(xoa);
            loaiPhong.removeChiTietGia(xoa);

            this.conn.getConn().delete(xoa);
            //  this.conn.getConn().store(giaPhong);

            return true;
        } catch (Db4oIOException | DatabaseClosedException | DatabaseReadOnlyException | HeadlessException ex) {
            return false;
        }
    }
}
